package org.seedltd.upms.controller.sys;

import lombok.AllArgsConstructor;
import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.validation.Save;
import org.seedltd.core.validation.Update;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.sys.entity.SysDict;
import org.seedltd.upms.model.sys.form.SysDictForm;
import org.seedltd.upms.model.sys.form.SysDictSearchForm;
import org.seedltd.upms.model.sys.vo.DictEnumVO;
import org.seedltd.upms.model.sys.vo.SysDictVO;
import org.seedltd.upms.service.sys.SysDictService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 字典管理控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sysDict")
public class SysDictController extends BaseController<SysDictService> {

    /**
     * 新增
     * 
     * @param form
     *            字典管理表单数据
     * @return 字典管理主键
     */
    @SysLog("新增字典")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('sys:dict:add')")
    public Long add(@RequestBody @Validated({Save.class}) SysDictForm form) {
        SysDict pojo = baseService.add(form);
        return pojo.getDictId();
    }

    /**
     * 获取详情
     * 
     * @param dictId
     *            字典管理主键
     * @return 字典管理
     */
    @GetMapping(value = "/get")
    public SysDictVO get(Long dictId) {
        Assert.notNull(dictId, "参数不能为空");
        SysDict pojo = baseService.findById(dictId);
        Assert.notNull(pojo, "未找到数据");
        SysDictVO pojoVo = new SysDictVO();
        BeanUtils.copyProperties(pojo, pojoVo);
        return pojoVo;
    }

    /**
     * 修改
     * 
     * @param form
     *            字典管理表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @SysLog("修改字典")
    @PostMapping(value = "/update")
    @PreAuthorize("hasAuthority('sys:dict:update')")
    public boolean update(@RequestBody @Validated({Update.class, Save.class}) SysDictForm form) {
        return baseService.update(form);
    }

    /**
     * 删除
     * 
     * @param dictId
     *            字典管理主键
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @SysLog("删除字典")
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    public boolean delete(Long dictId) {
        Assert.notNull(dictId, "ID不能为空");
        SysDict sysDict = baseService.findById(dictId);
        if (sysDict == null) {
            return true;
        }
        return baseService.delete(sysDict.getDictType(), dictId);
    }

    /**
     * 通过类型获取字典数据
     *
     * @param type
     *            字典类型
     */
    @GetMapping("/getByType")
    public List<DictEnumVO> getByType(String type) {
        return baseService.listSimpleByType(type);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 字典管理列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<SysDictVO> page(@RequestBody SysDictSearchForm searchForm) {
        return baseService.page(searchForm);
    }

}